/**
 * 所有的ChatMsg缓存. 
 */
var ChatMsgStore = function(){
    this.on('addItem', function(topic){
        //更新dashboard最新消息.
        chatGroupList.updateLastMsg(topic);
        //将消息移送到第一项.
        var $sitem = sessionList.$wrapper.find('.sessionListItem[sid={0}]'.format(topic['teid']));
        if($sitem.isExist()){
            try{
                //sessionList.addOrderly($sitem);
                $sitem.prependTo(sessionList.$wrapper);
            } catch(e){}
        }
    });
    /**
     * 收到消息后更新当前chat view 
     */
    this.on('addItem', function(topic){
        if(topic['local']) return;
        var isSelf = ujs.util.session.isCurrentUser(topic['user']['id']); 
        var isActiveChatPanel = ChatPanel.isActiveChatPanel(topic['teid']); 
        
        if(page.isActive){
            if(isActiveChatPanel){
                ChatPanel.getActiveChatView().addTopic(topic);            
                page.biz.signMsgAsReaded(topic['teid'], topic['seq'], function(msg){});
            } else {
                if(!isSelf){
                    page.lastMsg = topic['user']['name'] + ": " + ChatMsg.getMsgDesp(topic);                     
                }
            }                
        } else {
            //非自己的消息，需要通知。
            if(isSelf){           
                if(isActiveChatPanel){
                    //非active下, 当前session的消息暂不显示.当active激活时再显示.
                    //ChatPanel.getActiveChatView().addTopic(topic);            
                    //page.biz.signMsgAsReaded(topic['teid'], topic['seq'], function(msg){});
                }
            } else {
                page.lastMsg = topic['user']['name'] + ": " + ChatMsg.getMsgDesp(topic);
            }
        }
    });     
    /**
     * 收到消息后更新session list.
     */                                   
    this.on('addItem', function(topic){
        var isSelf = ujs.util.session.isCurrentUser(topic['user']['id']);     
        var isActiveChatPanel = ChatPanel.isActiveChatPanel(topic['teid']);
           
        sessionList.updateItem(topic['teid'], function(teObj){
            teObj['latestSeq'] = teObj['latestSeq'] || 0;         
            if(teObj['latestSeq'] < topic['seq']){
                teObj['lastMsg'] = ChatMsg.getMsgDesp(topic);
                teObj['lastMsgUserName'] = topic['user']['name'];
                teObj['latestSeq'] = topic['seq'];
                teObj['msgTime'] = topic['createTime'];
                teObj['updateTime'] = topic['createTime'];
            }
            
            if((page.isActive && isActiveChatPanel) || topic['local'] || isSelf){
                if(teObj['currentSeq'] < topic['seq'])
                   teObj['currentSeq'] = topic['seq'];
            }
            if(isSelf){
                sessionList.$wrapper.find('.current').removeClass('blink');
                sessionList.$wrapper.find('.current').find('.unreadMsgCount').remove();
            }    
        });      
    });
    /**
     * 检测用户icon. 
     */
    this.on('addItem', function(topic){
        var isSelf = ujs.util.session.isCurrentUser(topic['user']['id']); 
        //若是SingleChat且icon已改变，更新ChatGroupICON.
        var teid = topic['teid'];
        if(teid){
            var chatGroupEntity = page.chatGroupStore.getItem(teid);
            if(chatGroupEntity['type'] === 'singlechat'){
                if(topic['user'] && !isSelf){
                    var userIcon = topic['user']['icon'];
                    if(chatGroupEntity['icon'] != userIcon){
                        chatGroupEntity['icon'] = userIcon;
                        chatGroupList.cardList.$wrapper.find('.cardWrapper[card-id={0}] .cardIcon img'.format(teid)).attr('src', ujs.browser.loc.addQuery(userIcon, {w:200}));
                        sessionList.$wrapper.find('.sessionListItem[sid={0}] .sessionIcon img'.format(teid)).attr('src', userIcon);
                        sessionList.$wrapper.find('.sessionListItem[sid={0}] .sessionTitle'.format(teid)).text(topic['user']['name']);
                        if(ChatPanel.isActiveChatPanel(teid)){
                            var chatPanel = ChatPanel.getActiveChatPanel();
                            chatPanel.$wrapper.find('.chatViewerIcon img').attr('src', ujs.browser.loc.addQuery(userIcon, {w:200}));
                            
                            var $chatMsg = chatPanel.$wrapper.find('.chatMsgItem:not(.right)');
                            $chatMsg.find('.chatUserIcon img').attr('src', userIcon);
                            
                            chatPanel.$wrapper.find('.chatViewerTitle').text(topic['user']['name']);
                        }
                    }
                }
            }
        }
    });  
    /**
     * 收到消息后更新当前chat view 
     */
    this.on('addItem', function(topic){
        if(topic['local']) return;
        var isSelf = ujs.util.session.isCurrentUser(topic['user']['id']); 
        var isActiveChatPanel = ChatPanel.isActiveChatPanel(topic['teid']); 
        
        if(page.isActive){
            if(!isActiveChatPanel){
                if(!isSelf){
                    page.setUnread(true);
                }
            }
        } else {
            //非自己的消息，需要通知。
            if(!isSelf){
                page.setUnread(true);
            }
        }
    });  
}.extend(Store);